package cc.rengu.igas.bomp.common.enums;

import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统参数枚举
 * Created by wukf
 *
 * @author tt
 * @version 1.0
 * @date 2019/08/15
 */
public enum SysParamEnum {

    localStorageRootPath("IMAGE_STORAGE_PARAM", "localStorageRootPath", "服务器本地图片存储路径"),
    realNameQrcodePath("IMAGE_STORAGE_PARAM", "realNameQrcodePath", "实名认证二维码图片存储路径"),

    CORE_ACCT_SIGN("MCHNT_SERVICE", "CORE_ACCT_SIGN", "是否需要进行核心签约"),
    SIX_CHECK_FLAG("MCHNT_SERVICE", "SIX_CHECK_FLAG", "六要素(营业执照类型+营业执照号+证件类型+证件号+手机号+结算账户)校验标志"),
    OFFLINE_LICENCE_CHECK_FLAG("MCHNT_SERVICE", "OFFLINE_LICENCE_CHECK_FLAG", "线上商户不校验营业执照签约商户个数"),
    OFFLINE_ACCT_CHECK_FLAG("MCHNT_SERVICE", "OFFLINE_ACCT_CHECK_FLAG", "线下商户重复信息校验结算账号"),
    RESERVE_FUND_FLAG("MCHNT_SERVICE", "RESERVE_FUND_FLAG", "是否允许开通备付金标志"),
    MULTILEVEL_AUDIT_FLAG("MCHNT_SERVICE", "MULTILEVEL_AUDIT_FLAG", "开通多级(二级)审核标志"),
    IMAGE_PLAT_FORM_FLAG("MCHNT_SERVICE", "IMAGE_PLAT_FORM_FLAG", "影像平台开通标志"),
    WECHAT_REAL_NAME_APPLYMENT_FLAG("MCHNT_SERVICE", "WECHAT_REAL_NAME_APPLYMENT_FLAG", "微信实名认证业务开通标志"),
    SUPPORT_DO_REFUND("MCHNT_SERVICE", "SUPPORT_DO_REFUND", "D0退款开通标志"),
    UNION_FLASH_FLAG("MCHNT_SERVICE", "UNION_FLASH_FLAG", "银联云闪付特殊费率"),
    REAL_NAME_FLAG("MCHNT_SERVICE", "REAL_NAME_FLAG", "实名认证开关"),

    COPS_NOTIFY_URL("{mchntExpandChannel}", "COPS_NOTIFY_URL", "能力外放推送notifyUrl"),
    E_FAMILY_AGENT_NO("E_FAMILY", "AGENT_NO", "齐鲁E家亲-代理商商户号"),


    ACTIVITY_LIST("WECHAT_ACTIVITY_RATE", "ACTIVITY_LIST", "微信特殊费率-活动列表"),
    SUPPORT_SUBJECT_TYPE("{activityId}", "SUPPORT_SUBJECT_TYPE", "微信特殊费率-活动支持的主体类型+结算ID"),
    ACTIVITY_RATE("{activityId}", "ACTIVITY_RATE", "微信特殊费率-活动费率"),
    MATERIAL_LIST("{activityId}-{wechatMchntType}", "MATERIAL_LIST", "微信特殊费率-材料列表"),


    ;

    public static SysParam getSysParam(String instId, SysParamEnum sysParamEnum) throws Exception {
        return getSysParam(instId, sysParamEnum.getParamType(), sysParamEnum.getParamKey());
    }

    public static SysParam getSysParam(String instId, String paramType, String paramKey) throws Exception {
        RgLogger rglog = RgLog.getLogger(SysParamEnum.class.getName());
        rglog.info("尝试获取参数type<{}>,key<{}>", paramType, paramKey);
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(instId, paramType, paramKey);
        if (null == sysParam) {
            rglog.error("无此系统参数或参数失效.");
            return null;
        }
        rglog.info("获取参数:" + JSONObject.toJSONString(sysParam));
        if (!AppParamConstant.YES.equals(sysParam.getParamStatus())) {
            rglog.error("此参数状态<{}>已失效.", sysParam.getParamStatus());
            return null;
        }
        return sysParam;
    }

    private static String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    public static List<SysParam> getSysParamListByParamKey(String instId, String paramKey) throws Exception {
        RgLogger rglog = RgLog.getLogger(SysParamEnum.class.getName());
        // XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        // String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        // if (null != corporation && !corporation.isEmpty()) {
        //     String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
        //     dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        // }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = "        SELECT\n" +
                "        INST_ID,\n" +
                "        PARAM_TYPE,\n" +
                "        PARAM_KEY,\n" +
                "        PARAM_VALUE,\n" +
                "        PARAM_STATUS,\n" +
                "        PARAM_DESC,\n" +
                "        CREATE_TIME,\n" +
                "        UPDATE_TIME,\n" +
                "        REMARK1,\n" +
                "        REMARK2\n" +
                "        INTO\n" +
                "        #instId,\n" +
                "        #paramType,\n" +
                "        #paramKey,\n" +
                "        #paramValue,\n" +
                "        #paramStatus,\n" +
                "        #paramDesc,\n" +
                "        #createTime,\n" +
                "        #updateTime,\n" +
                "        #remark1,\n" +
                "        #remark2\n" +
                "        FROM T_P_SYS_PARAM\n" +
                "        WHERE\n" +
                "        INST_ID        =$instId          AND" +
                "        PARAM_STATUS   =$paramStatus     AND" +
                "        PARAM_KEY      =$paramKey\n";
        SysParam sysParam = new SysParam();
        sysParam.setInstId(instId);
        sysParam.setParamStatus(AppParamConstant.YES);
        sysParam.setParamKey(paramKey);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, sysParam, SysParam.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据instId:<{}>,paramKey:<{}>查询系统参数表信息失败", instId, paramKey);
            if (!AppParamConstant.DEFAULT_INSTID.equals(instId)) {
                return getSysParamListByParamKey(AppParamConstant.DEFAULT_INSTID, paramKey);
            } else {
                return null;
            }
        }

        return objectList.stream().map(item -> (SysParam) item).collect(Collectors.toList());
    }

    @Override
    public String toString() {
        return "<" + this.getParamKey() + ">-<" + this.getParamType() + ">";
    }

    SysParamEnum(String paramType, String paramKey, String paramDesc) {
        this.paramType = paramType;
        this.paramKey = paramKey;
        this.paramDesc = paramDesc;
    }

    private String paramType;
    private String paramKey;
    private String paramDesc;

    public String getParamType() {
        return paramType;
    }

    public String getParamKey() {
        return paramKey;
    }

    public String getParamDesc() {
        return paramDesc;
    }
}
